package lab2;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class ext5 {
	private String file;
	ext5(String file) throws IOException{
		this.file = file;
		this.para();
	}
	public void para() throws IOException{
		HashMap<String,Integer> map = new HashMap<String,Integer>();
        BufferedReader fin = new BufferedReader(new FileReader(this.file));
        String line = null;
        while((line=fin.readLine())!=null){
            String [] words = line.split(" ");
            for(int i=0;i<words.length;i++){
            	// filter some invalid words
            	words[i] = words[i].replace(",", "");
            	words[i] = words[i].replace(".", "");
            	words[i] = words[i].replace("-", "");
            	words[i] = words[i].replace(":", "");
            	// put the word into the map
            	if(map.containsKey(words[i])){
            		Integer count =  map.get(words[i])+1;
            		map.put(words[i],count);
            	}else{
                	map.put(words[i], 1);
            	}
            }
            for(Map.Entry<String, Integer> entry : map.entrySet()){
            	System.out.println(entry.getKey() + " " + entry.getValue());
            }
        }
        fin.close();

        
	}
}
